<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class admin extends CI_Controller{
	public function __construct(){
		parent::__construct();
		if(!isset($_SESSION["isadmin"]) || !$_SESSION["isadmin"]){
			header("location:/index.php/login");
		}
	}
	
	public function index(){
		$username = isset($_GET['username'])?$_GET['username']:"";
		$this->load->model("AdminModel");		
		$where = $username?"where username like '%$username%'":"";
	
		$this->config->load('pagination',true);
		$pagesize  = $this->config->item("per_page","pagination");
		$sposition  = isset($_GET['per_page']) && (int)$_GET['per_page'] ? (int)$_GET['per_page'] : 0;
		$sql = "select * from {$this->AdminModel->getTable()} $where order by id desc limit $sposition,$pagesize";
		$data['adminlist'] = $this->AdminModel->getAll($sql);
	
		//分页
		$this->load->library('pagination');
		$config['base_url'] = "index.php?c=admin&a=index&username=$username";
		$config['total_rows'] = $this->AdminModel->getCount($where);
		$this->pagination->initialize($config);
		$data['page'] = $this->pagination->create_links();
		$data['super']    = isset($_SESSION['super'])?$_SESSION['super']:0;
		$data['username'] = $username;
		$this->load->view("header");
		$this->load->view("admin_index",$data);
		$this->load->view("footer");
	}
	
	public function add(){
		if($_POST){			
			$this->load->model("AdminModel");
			$_POST['id'] = $this->AdminModel->getId();
			//验证用户名唯一性
			if($this->AdminModel->isExistName($_POST['username'])){
				mcmessage_error("登陆名已被注册","./index.php?c=admin&a=add");
			}
			$newData = array('id'=>$_POST['id'],
					'username'=>$_POST['username'],
					'salt' =>  mcRandStr(10),
					"created"=>time()
			);					
			if($_POST['password'] != $_POST['confirm_password']){
				mcmessage_error("两次输入的密码不一致","./index.php?c=admin&a=edit");				
			}
			$newData['password'] = $this->common->md5Password($_POST['password'],$newData['salt']);
			$this->AdminModel->insert($newData);						
			header("location:index.php?c=admin&a=index");
		}
		$data = array();
		$this->load->view("header");
		$this->load->view("admin_add",$data);
		$this->load->view("footer");
	}
	/**
	 * 修改其他管理员信息
	 */
	public function editother(){
		if(!isset($_SESSION["super"]) || !$_SESSION["super"]){
			mcmessage_error("没有编辑管理员权限","./index.php?c=admin&a=index");
		}
		$adminId = isset($_GET['id'])?$_GET['id']:null;
		//$adminId = $_GET['id'];
		$this->load->model("AdminModel");
		$data['admin'] = $this->AdminModel->getRow("select * from {$this->AdminModel->getTable()} where id = '{$adminId}'");
		if(empty($data['admin'])){ //用户不存在
			header("location:index.php?c=login&a=index");return;
		}
		if($_POST){
			$this->load->model("AdminModel");
			//验证用户名唯一性
			if($this->AdminModel->isExistName($_POST['username'],$adminId)){
				mcmessage_error("登陆名已被注册","./index.php?c=admin&a=index");
			}
			$newData = array('id'=>$adminId,'username'=>$_POST['username']);
			if(empty($_POST['password'])){//修改登陆名
				$this->AdminModel->update($newData);
			}elseif($_POST['password'] != $_POST['confirm_password']){
				mcmessage_error("两次输入的密码不一致","./index.php?c=admin&a=index");
				//$_POST['password'] = $this->common->md5Password($_POST['password'],$data['admin']['salt']);
			}else{
				$newData['password'] = $this->common->md5Password($_POST['password'],$data['admin']['salt']);
				$this->AdminModel->update($newData);
			}
			unsetLoginCookie();
			mcmessage_success("修改成功","./index.php?c=admin&a=index");
		}
		
		$this->load->view("header");
		$this->load->view("admin_editother",$data);
		$this->load->view("footer");
	}
	/**
	 * 编辑个人信息
	 */
	public function edit(){	
		
		$adminId = isset($_SESSION['adminid'])?$_SESSION['adminid']:null;
		//$adminId = $_GET['id'];
		$this->load->model("AdminModel");
		$data['admin'] = $this->AdminModel->getRow("select * from {$this->AdminModel->getTable()} where id = '{$adminId}'");	
		if(empty($data['admin'])){ //用户不存在
			header("location:index.php?c=login&a=index");return;
		}
		if($_POST){
			$this->load->model("AdminModel");
			//验证用户名唯一性
			if($this->AdminModel->isExistName($_POST['username'],$adminId)){
				mcmessage_error("登陆名已被注册","./index.php?c=admin&a=edit");
			}
			$newData = array('id'=>$adminId,'username'=>$_POST['username']);
			if(empty($_POST['password'])){//修改登陆名   
				$this->AdminModel->update($newData);
			}elseif($_POST['password'] != $_POST['confirm_password']){
				mcmessage_error("两次输入的密码不一致","./index.php?c=admin&a=edit");
				//$_POST['password'] = $this->common->md5Password($_POST['password'],$data['admin']['salt']);
			}elseif($this->common->md5Password($_POST['old_password'],$data['admin']['salt']) != $data['admin']['password']) {
				mcmessage_error("原始密码不正确","./index.php?c=admin&a=edit");
			}else{
				$newData['password'] = $this->common->md5Password($_POST['password'],$data['admin']['salt']);
				$this->AdminModel->update($newData);				
			}
			unsetLoginCookie();
			mcmessage_success("修改成功","./index.php?c=admin&a=edit");			
		}
		
		$this->load->view("header");
		$this->load->view("admin_edit",$data);
		$this->load->view("footer");
	}
	
	/**
	 * @todo 删除一条商品信息
	 */
	public function del(){
		if(!isset($_SESSION["super"]) || !$_SESSION["super"]){
			mcmessage_error("没有删除管理员权限","./index.php?c=admin&a=index");
		}
		$id = isset($_GET['id']) && is_numeric($_GET['id'])?$_GET['id']:null;		
		$this->load->model("AdminModel");
		$adminInfo = $this->AdminModel->info($id);
		if($adminInfo['super']){
			mcmessage_error("不能删除超级管理员","./index.php?c=admin&a=index");
		}
		$this->AdminModel->delete($id);
		mcmessage_success("删除成功","./index.php?c=admin&a=index");
		//echo json_encode(array('r'=>"SUCCESS",'d'=>"删除成功!"));exit;
	}
}